<?php

namespace App\Http\Controllers\settingcontroller;
use App\Http\Controllers\Controller;
use App\Models\LogTaskSettings;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Pojo\Response;
use App\Exceptions\BusinessException;
class LogSettingController extends Controller{
    public function getLogSetting(Request $request){
        $logSetting = LogTaskSettings::where('user_id', Auth::id())->whereNull('deleted_at')->first();
        if(is_null($logSetting)){
            throw new BusinessException("请设置日志任务，才能进行后续操作");
        }
        return Response::success($logSetting, "打卡任务已设置");
    }

    public function saveLogSetting(Request $request){
    //    LogTaskSettings
    $data = $request->validate([
        'default_images' => 'required|boolean',
        'imageCount' => 'required|integer',
        'logType' => 'required|string',
        'randomLog' => 'required|boolean',
    ]);

    $logSetting = LogTaskSettings::where('user_id', Auth::id())->whereNull('deleted_at')->first();

    if(is_null($logSetting)){
        // 没有对应的数据 则创建
        $logSetting = new LogTaskSettings();
        $logSetting->user_id = Auth::id();
        $logSetting->image_count = $data['imageCount'];
        $logSetting->default_images = $data['default_images'];
        $logSetting->random_log = $data['randomLog'];
        $logSetting->log_type = $data['logType'];
        $logSetting->save();
        return Response::success($logSetting, "日志任务已保存");
    }
    $logSetting->update([
        'user_id' => Auth::id(),
        'default_images' => $data['default_images'],
        'log_type' => $data['logType'],
        'image_count' => $data['imageCount'],
        'random_log' => $data['randomLog']
    ]);
    return Response::success($logSetting, "日志任务已更新");

    }

}